package com.wman.sheep.widget.dialog;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.wman.sheep.R;
import com.wman.sheep.common.imageloder.ImageLoader;

public class DialogViewHolder {
    private final SparseArray<View> mViews;
    private View mDialogView;

    private DialogViewHolder(Context context, int layoutId) {
        this.mViews = new SparseArray<View>();
        mDialogView = View.inflate(context, layoutId, null);
    }

    public static DialogViewHolder get(Context context, int layoutId) {
        return new DialogViewHolder(context, layoutId);
    }

    public View getConvertView() {
        return mDialogView;
    }

    /**
     * Set the string for TextView
     *
     * @param viewId
     * @param text
     * @return
     */
    public DialogViewHolder setText(int viewId, CharSequence text) {
        if (StringUtils.isEmpty(text.toString())) {
            text = "";
        }
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public DialogViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public DialogViewHolder setUrlImageView(Context context, int viewId, String url) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayImage(context, url, iv, R.drawable.sample_placeholder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public DialogViewHolder setUrlImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayImage(context, url, iv, placeHolder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 圆形图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @return
     */
    public DialogViewHolder setUrlCircleImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayCircleImage(context, url, iv, placeHolder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 圆角图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @return
     */
    public DialogViewHolder setUrlRoundImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayRoundImage(context, url, iv, placeHolder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 圆角图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @param radius
     * @return
     */
    public DialogViewHolder setUrlRoundImageView(Context context, int viewId, String url, int placeHolder, int radius) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayRoundImage(context, url, iv, placeHolder, radius);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 模糊图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @return
     */
    public DialogViewHolder setUrlBlurImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayBlurImage(context, url, iv, placeHolder, 10);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 模糊图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @param radius
     * @return
     */
    public DialogViewHolder setUrlBlurImageView(Context context, int viewId, String url, int placeHolder, int radius) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayBlurImage(context, url, iv, placeHolder, radius);

        return this;
    }

    /**
     * set view visible
     *
     * @param viewId
     * @return
     */
    public DialogViewHolder setViewInViSible(int viewId) {
        TextView view = getView(viewId);
        view.setVisibility(View.INVISIBLE);
        return this;
    }

    /**
     * set view visible
     *
     * @param viewId
     * @return
     */
    public DialogViewHolder setViewViSible(int viewId) {
        TextView view = getView(viewId);
        view.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * set view gone
     *
     * @param viewId
     * @return
     */
    public DialogViewHolder setViewGone(int viewId) {
        TextView view = getView(viewId);
        view.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置点击
     */
    public DialogViewHolder setOnClick(int viewId, OnClickListener onClick) {
        View view = getView(viewId);
        view.setOnClickListener(onClick);
        return this;
    }

    /**
     * Through control the Id of the access to control, if not join views
     *
     * @param viewId
     * @return
     */

    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mDialogView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
}
